package com.herch.system.task;

import com.herch.common.utils.CommonUtil;
import com.herch.system.domain.HqEquipmentInfo;
import com.herch.system.mapper.HqEquipmentInfoMapper;
import com.herch.system.service.impl.SerialPortServiceImpl;
import gnu.io.SerialPort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: fei.li02
 * @Date: 2023/12/26 11:17
 * @Description:串口通信定时任务
 */
@Component
@Configuration
@EnableScheduling
public class SerialPortTask {
    private static final Logger log = LoggerFactory.getLogger(SerialPortTask.class);
    public static boolean isRunning = false;
    @Resource
    private HqEquipmentInfoMapper hqEquipmentInfoMapper;
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @Scheduled(fixedRate = 1000)
    @MessageMapping("/task")
    public void task() {
        Map<String,Object> map = new HashMap<>();
        if (isRunning) {
            try {
                SerialPort rs485MSerialport = SerialPortServiceImpl.rs485MSerialport;
                SerialPort fz420MSerialport = SerialPortServiceImpl.fz420MSerialport;
                OutputStream rs485OutputStream = rs485MSerialport.getOutputStream();
                OutputStream fz420OutputStream = fz420MSerialport.getOutputStream();
                List<HqEquipmentInfo> equipmentList = hqEquipmentInfoMapper.selectFrameStruList(0);//0代表串口
                for(HqEquipmentInfo equipmentInfo:equipmentList){
                    String rsFrameStru = equipmentInfo.getRsFrameStru();
                    String fzFrameStru = equipmentInfo.getFzFrameStru();
                    byte[] rsBytes = CommonUtil.hexStringToByteArray(rsFrameStru);
                    byte[] fzBytes = CommonUtil.hexStringToByteArray(fzFrameStru);
                    rs485OutputStream.write(rsBytes);
                    rs485OutputStream.flush();
                    Thread.sleep(500);
                    fz420OutputStream.write(fzBytes);
                    map.put("rsFrameStru",equipmentInfo.getEquipmentName()+">>>"+rsFrameStru);
                    map.put("fzFrameStru",equipmentInfo.getEquipmentName()+">>>"+fzFrameStru);
                    simpMessagingTemplate.convertAndSend( "/data/task", map);
                    fz420OutputStream.flush();
                    Thread.sleep(500);
                }

            } catch (IOException e) {
                log.error("Task outputStream 流发生异常:"+e.getMessage());
            } catch (InterruptedException e) {
                log.error("Task Thread.sleep 发生异常:"+e.getMessage());
            }
        }
    }

}
